package thread_design;

/**
 * Created with IDEA
 * author:ju bai
 * Date:2021/5/25
 * Time:20:09
 * 单例模式-饿汉方式
 * 优点：不用加锁也是线程安全的
 **/

class Singleton {
    //1.先创建一个私有的构造函数,防止其他类直接创建
    private Singleton(){

    }
    //2.定义私有变量
    private static Singleton singleton = new Singleton();

    //3.提供公共的获取实例的方法
    public static Singleton getInstance() {
        return singleton;
    }
}
public class Demo1 {


    public static void main(String[] args) {
        Singleton singleton = Singleton.getInstance();
        System.out.println(singleton);
    }
}
